// VARIABILI UTENTE

/* Livello di LOGGING:
	0: Nessun avviso (Default).
	1: Solo errori di connessione;
	2: Warning per eventuali problemi;
	3: Informazioni supplementari sui dati inviati;
	4: Informazioni su qualsiasi tipo di attività dell'estensione;
 */
var logLevel = 0;


// VARIABILI DI SISTEMA
var retrieveType = 'html-json';
var applicationKey = 'cf5557e9e1ed41683e1408aefaeeb4c6ee23096b';
var YOUTUBE_REGEXP = /youtube.*v[^\w]([\w-]+)/;
var VIMEO_REGEXP = /vimeo.com.\d/;

//returns the complete URL for retrieve informations from meemi
function getMeemiAPIURL(page,user,filters){
	
	url = "http://meemi.com/";
	
	if(!page) page = '';
	else if(!isNaN(page)){
		memeId = page;
		page = 'id_meme';
	}
	
	switch(page){
		//simple urls:  http://meemi.com/api3/p/version
		case 'version':
		case 'meme-sfera':
		case 'private':
		case 'private_sent':
		case 'favourites':
		case 'draft':
		case 'only_new_memes':
		case 'only_new_replies':
		case 'only_new_memes_priv':
		case 'only_new_replies_priv':
		case 'only_new_mentions':
		case 'only_new_followers':
			url += "api3/p/" + page;
			break;

		case 'notify':
			url += "api3/p/" + page;
			retrieveType = 'json';
			break;

		//urls with user not mandatory
		case 'curiosity':
		case 'best-of':
			if(user)
				url += "api3/" + user + "/" + page;
			else
				url += "api3/p/" + page;
			break

		//urls with mandatory user
		case 'info':
		case 'profile':
		case 'followings':
		case 'followers':
		case 'connected-today':
		case 'cloud-channels':
			url += "api3/" + user + "/" + page;
			break;
		
		//user url (without page)
		case '':
			url += "api3/" + user;
			break;
		
		//single meme url
		//to get the replies simply add "/replies" in filters
		case 'id_meme':
			url += "api3/" + user + "/" + memeId;
			break;

		//url that not returns any output to parse (only an OK)
		case 'save':          //meme_id passed via POST
			url += "api/p/" + page;
			retrieveType = false;
			break;
case 'notify':
		case 'reply':         //meme_id passed via POST
		case 'mark':          //use filters to specify what to mark
		case 'reshare':       //use filters to specify what to reshare
		case 'unreshare':     //use filters to specify what to unreshare
		case 'fav':           //use filters to specify what to mark favourite
		case 'pause':         //use filters to specify what to pause
		case 'play':          //use filters to specify what to play
		case 'convert_draft': //use filters to specify what to convert
		case 'follow':        //use filters to specify who to follow
		case 'unfollow':      //use filters to specify who to unfollow
		case 'block':         //use filters to specify who to block
		case 'unblock':       //use filters to specify who to unblock
			url += "api3/p/" + page;
			//retrieveType = false;
			break;
	}
	
	if(filters) url += filters;
	if(retrieveType) url += "/" + retrieveType;
	
	writeLog( "Requested API3 url: " + url, LOG_INFO );
	
	return url;
}

/* Log Priority manager.
 * Log on Console only if the Priority level is Higher or Equal than logLevel setting.
 * Using consants to better management.
 */
 
// Constants for logLevel:
var LOG_ERROR = 1;
var LOG_WARNING = 2;
var LOG_INFO = 3;
var LOG_MOREINFO = 4;

function writeLog( logText, logPriority ) {
	// Appending hour
	var now = new Date();
	logText = "[" + 
			now.getHours() + ":" + 
			now.getMinutes() + ":" + 
			now.getSeconds() + 
		"] " + 
		logText;
	if( logPriority <= logLevel ) {
		if( logPriority == LOG_ERROR )
			console.error( logText );
		if( logPriority == LOG_WARNING )
			console.warn( logText);
		if( logPriority >= LOG_INFO )
			console.log( logText );
	}
	
	if(logPriority <= LOG_ERROR){
		showUserMessage(logText,logPriority);
	}
}

function showUserMessage(text,type,timeout){
	messageStr = '';
	
	if(type=='error' || type==LOG_ERROR)
		messageStr += '<img src="icons/error.png" /> ';
	
	messageStr += text;
	
	messageStr += ' <a href="#"><img src="icons/cancel.png" onclick="javascript:$(\'#message-area\').fadeOut();"/></a>';
	$("#message-area").html(messageStr);
	$("#message-area").show();
	
	timeout = timeout?timeout:(10*1000);
	var errorTimer = window.setTimeout( function() {
		$("#message-area").fadeOut(); }
	, timeout );
}

